$(document).ready(function(){
	setDialog();
	closeDialog();
	//初始化表格
	$('#dataGridTable').datagrid({
		columns:[[ 
           {field:'noticeNumber',title:'版本号',width:100,align:'center'}, 
           {field:'noticeTime',title:'发布时间',width:130,align:'center'},
           {field:'createUser',title:'记录建立者',width:createUserWidth,align:'center'},
           {field:'createTime',title:'记录建立时间',width:createTimeWidth,align:'center'},
           {field:'lastModifiedUser',title:'记录最后修改者',width:lastModifiedWidth,align:'center'},
           {field:'lastModifiedTime',title:'记录最后修改时间',width:createTimeWidth,align:'center'}
         ]]
 	});
	$('#noticeNumber').textbox('textbox').keydown(function (e) {
		if (e.keyCode == 13) {
			$('#noticeNumber').textbox('setValue', $(this).val());
			queryNotice();
	    }
	});
});

//保存面板
function saveNotice(){
	var validateResult = $('#portalInfoDialogForm').form('validate');
	var noticeId=$("#noticeIdInfo").val();
	var noticeNumber=$("#noticeNumberInfo").val();
	var noticeContent=$("#noticeContentInfo").val();
	var noticeTime=$("#noticeTimeInfo").datetimebox('getValue');
	
	if(noticeNumber==null||noticeNumber==''){
		sysMsgShow("版本号不允许为空!");
		return false;
	}
	else if(noticeContent==null||noticeContent==''){
		sysMsgShow("更新内容不允许为空!");
		return false;
	}
	else if(noticeTime==null||noticeTime==''){
		sysMsgShow("发布时间不允许为空!");
		return false;
	}else{

		$.ajax({
			async : false,
			cache:false,
			type: 'post',
			dataType : "json",
			data : {
				"notice.noticeId" : noticeId,
				"notice.noticeNumber" : noticeNumber,
				"notice.noticeContent" : noticeContent,
				"notice.noticeTime" : noticeTime,
			},
			url: 'sys/saveNotice.action',
			error: function () {
				sysErrorShow('请求失败!');
			},success:function(data){
				var messgage = noticeId?"修改成功!":"添加成功!";
				if(data.success==true){
					reloadTable();
					closeDialog();
				}else{
					messgage = data.msg;
				}
				sysMsgShow(messgage);
			}
		});
	
	}	
}

//删除面板
function delNotice(){
	if($('#dataGridTable').datagrid('getSelected')){
		var ids = [];
		var cods = [];
		var selectedRow = $('#dataGridTable').datagrid('getSelections');
		for(var i=0;i<selectedRow.length;i++){
			ids.push(selectedRow[i].noticeId);
			cods.push(selectedRow[i].noticeNumber);
		}
		var noticeId = ids.join(',');
		$.messager.confirm('删除提示', '你确定删除下列公告吗?<br/><br/>'+cods.join(','), function(r){
				if (r){
					var url = 'sys/deleteNotice.action?notice.noticeId='+noticeId;
					$.ajax({
						async : false,
						cache:false,
						type: 'post',
						dataType : "json",
						url:url,
						error: function () {
							sysErrorShow('请求失败');
						},success:function(data){
							if(data.success==true){
								sysMsgShow("删除成功!");
							}else{
								sysErrorShow(data.msg);
							}
							reloadTable();
						}
					});
				}
		});
	}
	else
	{
		noSelectData();
	}
}

//查询
function queryNotice(){
	$("#dataGridTable").datagrid('reload',{		
	 	'notice.noticeNumber' : $("#noticeNumber").val(),
	}); 
}

/**--------------操作弹出框------------------**/
//初始化模窗
function setDialog(){
	$('#portalInfoDialog').dialog({
		modal: true,
		collapsible : true,
		resizable : true,
		onClose : function(){
			resetDialog();
		}
	});
}

//打开添加或修改模窗
function openDialog_addPortal(){ 
	$('#portalInfoDialog').dialog({
		closed: false,
		modal:true,
        title: "新增公告",
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: saveNotice
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#portalInfoDialog').dialog('close');                    
            }
        }]
    });
}

//打开编辑模窗
function openDialog_editPortal(){
	if($('#dataGridTable').datagrid('getSelected')){
		var noticeId = $('#dataGridTable').datagrid('getSelections')[0].noticeId;
		$.getJSON("sys/findNoticeById.action?notice.noticeId="+noticeId,null,
		function msg(json){
			$("#noticeIdInfo").val(noticeId);
			$("#noticeNumberInfo").textbox('setValue',json.object.noticeNumber);
			$("#noticeTimeInfo").datetimebox('setValue',json.object.noticeTime);
			editor1.html(json.object.noticeContent);
			editor1.sync();
//			$("#noticeContentInfo").val(json.object.noticeContent);
			openDialog_addPortal();
			$('#portalInfoDialog').dialog('setTitle','编辑公告'); 
	
		});
	}
	else
	{
		noSelectData();
	}
}

//关闭模窗
function closeDialog(){
	$('#portalInfoDialog').dialog('close');
}

//清空控件值
function resetDialog(){
	$("#noticeIdInfo").val('');
	$("#noticeNumberInfo").textbox('setValue','');
	$("#noticeTimeInfo").datetimebox('setValue','');
	if(editor1 != undefined){
		editor1.html('');
	}
}

//刷新表格
function reloadTable(){
	$('#dataGridTable').datagrid('reload');
}

//刷新
function reloadNotice()
{
	$("#noticeNumber").textbox('setValue','');
	queryNotice();
}
